package com.prothink.threadproject.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisTemplateConfig {
    //设置Redis序列化方式：
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        //设置一下链接工厂
        template.setConnectionFactory(factory);
        //创建字符串的序列化类
        RedisSerializer stringSerializer = new StringRedisSerializer();
        //将Key的序列化方式修改为字符串
        template.setKeySerializer(stringSerializer);
        //设置String value的序列化方式
        RedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();
        template.setValueSerializer(jsonSerializer);
        //设置Hash key的序列化为String
        template.setHashKeySerializer(stringSerializer);
        //设置Hash Value的序列化方式为json
        template.setHashValueSerializer(jsonSerializer);

        return template;
    }
}
